﻿Imports OpenQA.Selenium.Firefox
Imports CST = Vgate2BugZero.Common.Const
Public Class MainConvertBIZ
    Dim oBrowser As FirefoxDriver

    Public Sub New(ByVal voBrowser As FirefoxDriver)
        Me.oBrowser = voBrowser
        Me.LoginVgate()
    End Sub
    Public Function GetIssue(ByVal vsVgateNo As String) As Issue
        Dim oRet As New Issue
        Dim sTitleVgate As String = Me.GetTitleVgateIssue(vsVgateNo)
        oRet.VgateNo = vsVgateNo
        oRet.BugZeroType = Me.GetBugZeroType(vsVgateNo)
        oRet.BugZeroNo = Me.GetBugZeroNoByTitleVgate(sTitleVgate, oRet.BugZeroType)
        Return oRet
    End Function

    Public Function GetListIssue(ByVal arrVgateNo As List(Of String)) As List(Of Issue)
        Dim oRet As New List(Of Issue)
        For Each sVgateNo As String In arrVgateNo
            oRet.Add(Me.GetIssue(sVgateNo))
        Next
        Return oRet
    End Function
    Private Sub LoginVgate()
        oBrowser.Url = CST.URL.HOME
        oBrowser.Navigate()
        oBrowser.FindElementById("username").SendKeys("phongtq")
        oBrowser.FindElementById("password").SendKeys("12345678")
        oBrowser.FindElementByName("login").Submit()
    End Sub

    Private Function GetBugZeroType(ByVal vsTitleVgate As String) As CST.BugZeroType
        If vsTitleVgate.IndexOf(CST.BugZeroSignal.GetPrefixSignal(Common.Const.BugZeroType.BUGZERO_1)) Then
            Return Common.Const.BugZeroType.BUGZERO_1
        Else
            Return Common.Const.BugZeroType.BUGZERO_2
        End If
        Return Nothing
    End Function

    Private Function GetTitleVgateIssue(ByVal vsVgateNo As String) As String
        Dim sRetTitle As String = String.Empty
        oBrowser.Url = GetIssueUrl(vsVgateNo)
        oBrowser.Navigate()
        sRetTitle = oBrowser.Title
        Return sRetTitle
    End Function

    Private Function GetIssueUrl(ByVal vsVgateNo As String) As String
        Return CST.URL.ISSUE & vsVgateNo
    End Function

    Private Function GetBugZeroNoByTitleVgate(ByVal vsTitleVgate As String, ByVal eBugZeroType As CST.BugZeroType) As String
        Dim sBugZero As String = String.Empty
        Select Case eBugZeroType
            Case Common.Const.BugZeroType.BUGZERO_1
                Try
                    Dim iIndexPrefix As Integer = vsTitleVgate.IndexOf(CST.BugZeroSignal.GetPrefixSignal(Common.Const.BugZeroType.BUGZERO_1))
                    Dim iIndexSuffix As Integer = vsTitleVgate.IndexOf(CST.BugZeroSignal.GetSuffixSignal(Common.Const.BugZeroType.BUGZERO_1))
                    Dim iStart As Integer = iIndexPrefix + CST.BugZeroSignal.GetPrefixSignal(Common.Const.BugZeroType.BUGZERO_1).Length
                    Dim iEnd As Integer = iIndexSuffix + CST.BugZeroSignal.GetSuffixSignal(Common.Const.BugZeroType.BUGZERO_1).Length
                    sBugZero = vsTitleVgate.Substring(iStart, iEnd - iStart - 1)
                Catch ex As Exception

                End Try
            Case 2
                Try
                    Dim iIndexPrefix As Integer = vsTitleVgate.IndexOf(CST.BugZeroSignal.GetPrefixSignal(Common.Const.BugZeroType.BUGZERO_2))
                    Dim iIndexSuffix As Integer = vsTitleVgate.IndexOf(CST.BugZeroSignal.GetSuffixSignal(Common.Const.BugZeroType.BUGZERO_2))
                    Dim iStart As Integer = iIndexPrefix + CST.BugZeroSignal.GetPrefixSignal(Common.Const.BugZeroType.BUGZERO_2).Length
                    Dim iEnd As Integer = iIndexSuffix + CST.BugZeroSignal.GetSuffixSignal(Common.Const.BugZeroType.BUGZERO_2).Length
                    sBugZero = vsTitleVgate.Substring(iStart, iEnd - iStart - 1)
                Catch ex As Exception

                End Try
            Case Else

        End Select

        Return sBugZero
    End Function
End Class
